package com.screen.screendisplay.controller.emergency;

import com.screen.screendisplay.common.constant.Mn;
import com.screen.screendisplay.common.constant.ModuleTemplateName;
import com.screen.screendisplay.common.pojo.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * <p>
 * 能源消费表 前端控制器
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Tag(name = "应急-铝业产业", description = "铝业产业")
@RestController
@RequestMapping("/al-emerg")
public class EmergAlContrroller {

    @Value("${excelFilePath}")
    private String excelFilePath;

    /**
     * 铝业模板下载接口
     * @param response HttpServletResponse
     * @throws Exception 异常信息
     */
    @Operation(summary = "铝业模版下载", description = "铝业模版下载")
    @GetMapping("/download")
    public void downloadExcelTemplate(HttpServletResponse response) throws Exception {
        String templateFilePath = excelFilePath + ModuleTemplateName.AL_TEMPLATE_NAME;
        OutputStream os;
        try (InputStream inputStream = Files.newInputStream(Paths.get(templateFilePath))) {
            String filename = ModuleTemplateName.AL_TEMPLATE_NAME;
            response.setHeader("Content-Disposition", "attachment;filename=" + filename
                    + ";filename*=utf-8''" + URLEncoder.encode(filename, "UTF-8"));
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            os = response.getOutputStream();
            int len;
            byte[] b = new byte[Mn.M_1024_N];
            while ((len = inputStream.read(b, 0, b.length)) != -1) {
                os.write(b, 0, len);
            }
        }
        os.flush();
    }


    /**
     * 铝业模板导入
     * @param file 上传的文件
     * @return ApiResponse<ImportMsgVoResp> 操作结果信息
     * @throws Exception 异常信息
     */
    @Operation(summary = "铝业模版导入", description = "铝业模版导入")
    @PostMapping(value = "/upload")
    public ApiResponse<Object> reportUpload(
            @RequestParam(value="file") MultipartFile file) throws Exception {

        if(file.isEmpty()){
            return ApiResponse.failed("文件不存在");
        }
        if(!Objects.requireNonNull(file.getOriginalFilename()).contains(".xlsx")) {
            return ApiResponse.failed("文件类型不匹配,上传.xlsx类型的文件");
        }
//        return importFileService.standardUpload(file,rptId);
        return ApiResponse.succeed();
    }



}
